//
//  ViewController.m
//  通讯录
//
//  Created by mafeng on 16/7/25.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "AddViewController.h"
#import "StuInfoViewController.h"
#import "SQLiteDataBaseManager.h"
#define kReuse @"reuse"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, AddViewControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *stuArr;
@property (nonatomic, strong) SQLiteDataBaseManager *dataBaseManager;
@end

@implementation ViewController
    
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
//    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    // 从文件中读取联系人
     NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSLog(@"%@", [pathArray firstObject]);
     NSString *contactPath = [[pathArray firstObject]stringByAppendingPathComponent:@"DLS160710.plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:contactPath];
    self.stuArr = [array mutableCopy];
    NSLog(@"%@", array);
    
    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
  //  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kReuse];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}
    // 点击事件
- (void)click {
    AddViewController *addVC = [[AddViewController alloc] init];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
}
#pragma mark -- UITableViewDlegate
    // tableview的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.stuArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    StuInfoViewController *infoVC = [[StuInfoViewController alloc] init];
    infoVC.name = [self.stuArr[indexPath.row] objectForKey:@"name"];
    infoVC.gender = [self.stuArr[indexPath.row] objectForKey:@"gender"];
    infoVC.age = [self.stuArr[indexPath.row] objectForKey:@"age"];
    infoVC.phoneNumber = [self.stuArr[indexPath.row] objectForKey:@"phoneNumber"];
    [self.navigationController pushViewController:infoVC animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuse];
    // 要用系统的样式, 得用下面这种方法
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kReuse];
    }
    cell.textLabel.text = [self.stuArr[indexPath.row] objectForKey:@"name"];
    cell.imageView.image = [UIImage imageWithData:[_stuArr[indexPath.row] objectForKey:@"headImageData"]];
    cell.detailTextLabel.text = [self.stuArr[indexPath.row] objectForKey:@"phoneNumber"];
    return cell;
}
- (void)passValue:(NSDictionary *)value {
    // 将新联系人加入数组
    [self.stuArr addObject:value];
    NSLog(@"%@", self.stuArr);
    
    // 将数据存入本地
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@", [pathArray firstObject]);
    NSString *contactPath = [[pathArray firstObject]stringByAppendingPathComponent:@"DLS160710.plist"];
    BOOL isWrite = [self.stuArr writeToFile:contactPath atomically:YES];
    if (isWrite) {
        NSLog(@"写入成功");
    } else {
        NSLog(@"写入失败");
    }
    
    // 将数据写入数据库
    // 打开数据库
    _dataBaseManager = [SQLiteDataBaseManager shareManager];
    // 创建表
    [_dataBaseManager createTable];
    
    // 写入数据
    _dataBaseManager insertContact:<#(nonnull Contact *)#>
    // 关闭数据库
    
    // 刷新tableview
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
    
    @end
